<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Type encoding (Using the GNU Compiler Collection (GCC))</title>

<meta name="description" content="Type encoding (Using the GNU Compiler Collection (GCC))">
<meta name="keywords" content="Type encoding (Using the GNU Compiler Collection (GCC))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html#Top" rel="start" title="Top">
<link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Objective_002dC.html#Objective_002dC" rel="up" title="Objective-C">
<link href="Legacy-type-encoding.html#Legacy-type-encoding" rel="next" title="Legacy type encoding">
<link href="What-you-can-and-what-you-cannot-do-in-_002bload.html#What-you-can-and-what-you-cannot-do-in-_002bload" rel="prev" title="What you can and what you cannot do in +load">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en">
<a name="Type-encoding"></a>
<div class="header">
<p>
Next: <a href="Garbage-Collection.html#Garbage-Collection" accesskey="n" rel="next">Garbage Collection</a>, Previous: <a href="Executing-code-before-main.html#Executing-code-before-main" accesskey="p" rel="prev">Executing code before main</a>, Up: <a href="Objective_002dC.html#Objective_002dC" accesskey="u" rel="up">Objective-C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Type-Encoding"></a>
<h3 class="section">8.3 Type Encoding</h3>

<p>This is an advanced section.  Type encodings are used extensively by
the compiler and by the runtime, but you generally do not need to know
about them to use Objective-C.
</p>
<p>The Objective-C compiler generates type encodings for all the types.
These type encodings are used at runtime to find out information about
selectors and methods and about objects and classes.
</p>
<p>The types are encoded in the following way:
</p>

<table>
<tr><td width="25%"><code>_Bool</code></td><td width="75%"><code>B</code></td></tr>
<tr><td width="25%"><code>char</code></td><td width="75%"><code>c</code></td></tr>
<tr><td width="25%"><code>unsigned char</code></td><td width="75%"><code>C</code></td></tr>
<tr><td width="25%"><code>short</code></td><td width="75%"><code>s</code></td></tr>
<tr><td width="25%"><code>unsigned short</code></td><td width="75%"><code>S</code></td></tr>
<tr><td width="25%"><code>int</code></td><td width="75%"><code>i</code></td></tr>
<tr><td width="25%"><code>unsigned int</code></td><td width="75%"><code>I</code></td></tr>
<tr><td width="25%"><code>long</code></td><td width="75%"><code>l</code></td></tr>
<tr><td width="25%"><code>unsigned long</code></td><td width="75%"><code>L</code></td></tr>
<tr><td width="25%"><code>long long</code></td><td width="75%"><code>q</code></td></tr>
<tr><td width="25%"><code>unsigned long long</code></td><td width="75%"><code>Q</code></td></tr>
<tr><td width="25%"><code>float</code></td><td width="75%"><code>f</code></td></tr>
<tr><td width="25%"><code>double</code></td><td width="75%"><code>d</code></td></tr>
<tr><td width="25%"><code>long double</code></td><td width="75%"><code>D</code></td></tr>
<tr><td width="25%"><code>void</code></td><td width="75%"><code>v</code></td></tr>
<tr><td width="25%"><code>id</code></td><td width="75%"><code>@</code></td></tr>
<tr><td width="25%"><code>Class</code></td><td width="75%"><code>#</code></td></tr>
<tr><td width="25%"><code>SEL</code></td><td width="75%"><code>:</code></td></tr>
<tr><td width="25%"><code>char*</code></td><td width="75%"><code>*</code></td></tr>
<tr><td width="25%"><code>enum</code></td><td width="75%">an <code>enum</code> is encoded exactly as the integer type that the compiler uses for it, which depends on the enumeration
values.  Often the compiler users <code>unsigned int</code>, which is then encoded as <code>I</code>.</td></tr>
<tr><td width="25%">unknown type</td><td width="75%"><code>?</code></td></tr>
<tr><td width="25%">Complex types</td><td width="75%"><code>j</code> followed by the inner type.  For example <code>_Complex double</code> is encoded as &quot;jd&quot;.</td></tr>
<tr><td width="25%">bit-fields</td><td width="75%"><code>b</code> followed by the starting position of the bit-field, the type of the bit-field and the size of the bit-field (the bit-fields encoding was changed from the NeXT&rsquo;s compiler encoding, see below)</td></tr>
</table>


<p>The encoding of bit-fields has changed to allow bit-fields to be
properly handled by the runtime functions that compute sizes and
alignments of types that contain bit-fields.  The previous encoding
contained only the size of the bit-field.  Using only this information
it is not possible to reliably compute the size occupied by the
bit-field.  This is very important in the presence of the Boehm&rsquo;s
garbage collector because the objects are allocated using the typed
memory facility available in this collector.  The typed memory
allocation requires information about where the pointers are located
inside the object.
</p>
<p>The position in the bit-field is the position, counting in bits, of the
bit closest to the beginning of the structure.
</p>
<p>The non-atomic types are encoded as follows:
</p>

<table>
<tr><td width="20%">pointers</td><td width="80%">&lsquo;<samp>^</samp>&rsquo; followed by the pointed type.</td></tr>
<tr><td width="20%">arrays</td><td width="80%">&lsquo;<samp>[</samp>&rsquo; followed by the number of elements in the array followed by the type of the elements followed by &lsquo;<samp>]</samp>&rsquo;</td></tr>
<tr><td width="20%">structures</td><td width="80%">&lsquo;<samp>{</samp>&rsquo; followed by the name of the structure (or &lsquo;<samp>?</samp>&rsquo; if the structure is unnamed), the &lsquo;<samp>=</samp>&rsquo; sign, the type of the members and by &lsquo;<samp>}</samp>&rsquo;</td></tr>
<tr><td width="20%">unions</td><td width="80%">&lsquo;<samp>(</samp>&rsquo; followed by the name of the structure (or &lsquo;<samp>?</samp>&rsquo; if the union is unnamed), the &lsquo;<samp>=</samp>&rsquo; sign, the type of the members followed by &lsquo;<samp>)</samp>&rsquo;</td></tr>
<tr><td width="20%">vectors</td><td width="80%">&lsquo;<samp>![</samp>&rsquo; followed by the vector_size (the number of bytes composing the vector) followed by a comma, followed by the alignment (in bytes) of the vector, followed by the type of the elements followed by &lsquo;<samp>]</samp>&rsquo;</td></tr>
</table>

<p>Here are some types and their encodings, as they are generated by the
compiler on an i386 machine:
</p>
<br>

<table>
<tr><td width="60%">Objective-C type</td><td width="40%">Compiler encoding</td></tr>
<tr><td width="60%"><div class="smallexample">
<pre class="smallexample">int a[10];
</pre></div></td><td width="40%"><code>[10i]</code></td></tr>
<tr><td width="60%"><div class="smallexample">
<pre class="smallexample">struct {
  int i;
  float f[3];
  int a:3;
  int b:2;
  char c;
}
</pre></div></td><td width="40%"><code>{?=i[3f]b128i3b131i2c}</code></td></tr>
<tr><td width="60%"><div class="smallexample">
<pre class="smallexample">int a __attribute__ ((vector_size (16)));
</pre></div></td><td width="40%"><code>![16,16i]</code> (alignment depends on the machine)</td></tr>
</table>

<br>

<p>In addition to the types the compiler also encodes the type
specifiers.  The table below describes the encoding of the current
Objective-C type specifiers:
</p>
<br>

<table>
<tr><td width="25%">Specifier</td><td width="75%">Encoding</td></tr>
<tr><td width="25%"><code>const</code></td><td width="75%"><code>r</code></td></tr>
<tr><td width="25%"><code>in</code></td><td width="75%"><code>n</code></td></tr>
<tr><td width="25%"><code>inout</code></td><td width="75%"><code>N</code></td></tr>
<tr><td width="25%"><code>out</code></td><td width="75%"><code>o</code></td></tr>
<tr><td width="25%"><code>bycopy</code></td><td width="75%"><code>O</code></td></tr>
<tr><td width="25%"><code>byref</code></td><td width="75%"><code>R</code></td></tr>
<tr><td width="25%"><code>oneway</code></td><td width="75%"><code>V</code></td></tr>
</table>

<br>

<p>The type specifiers are encoded just before the type.  Unlike types
however, the type specifiers are only encoded when they appear in method
argument types.
</p>
<p>Note how <code>const</code> interacts with pointers:
</p>
<br>

<table>
<tr><td width="25%">Objective-C type</td><td width="75%">Compiler encoding</td></tr>
<tr><td width="25%"><div class="smallexample">
<pre class="smallexample">const int
</pre></div></td><td width="75%"><code>ri</code></td></tr>
<tr><td width="25%"><div class="smallexample">
<pre class="smallexample">const int*
</pre></div></td><td width="75%"><code>^ri</code></td></tr>
<tr><td width="25%"><div class="smallexample">
<pre class="smallexample">int *const
</pre></div></td><td width="75%"><code>r^i</code></td></tr>
</table>

<br>

<p><code>const int*</code> is a pointer to a <code>const int</code>, and so is
encoded as <code>^ri</code>.  <code>int* const</code>, instead, is a <code>const</code>
pointer to an <code>int</code>, and so is encoded as <code>r^i</code>.
</p>
<p>Finally, there is a complication when encoding <code>const char *</code>
versus <code>char * const</code>.  Because <code>char *</code> is encoded as
<code>*</code> and not as <code>^c</code>, there is no way to express the fact
that <code>r</code> applies to the pointer or to the pointee.
</p>
<p>Hence, it is assumed as a convention that <code>r*</code> means <code>const
char *</code> (since it is what is most often meant), and there is no way to
encode <code>char *const</code>.  <code>char *const</code> would simply be encoded
as <code>*</code>, and the <code>const</code> is lost.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="Legacy-type-encoding.html#Legacy-type-encoding" accesskey="1">Legacy type encoding</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="_0040encode.html#g_t_0040encode" accesskey="2">@encode</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Method-signatures.html#Method-signatures" accesskey="3">Method signatures</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<div class="header">
<p>
Next: <a href="Garbage-Collection.html#Garbage-Collection" accesskey="n" rel="next">Garbage Collection</a>, Previous: <a href="Executing-code-before-main.html#Executing-code-before-main" accesskey="p" rel="prev">Executing code before main</a>, Up: <a href="Objective_002dC.html#Objective_002dC" accesskey="u" rel="up">Objective-C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
